$(document).ready(function(){

    if(sessionStorage.getItem("isLogin")=="true"){
        $("#index-container").css({
            display:"none"
        })
    }else{
        $("#back").css({
            display:"none"
        })
    }
    if(window.location.pathname=='/newsDetail.html'||window.location.pathname=='/policyDetail.html'){
        var title=localStorage.getItem("title");
        var time=localStorage.getItem("time");
        var content=localStorage.getItem("content");
        $('#title').text(title);
        $('#time').text(time);
        $('#content').text(content);
    }else if(window.location.pathname=='/productsDetail.html'){
        var productItem=[JSON.parse(localStorage.getItem("productItem"))];
        console.log(productItem);
        var Limit=['100万及以下','200万及以下','300万及以下','500万及以下','1000万及以下','1000万以上'];
        var guaranteeMode=['抵押','信保基金','一般保证','信用','实际控制人夫妇提供个人连带担保'];
        var loanType=['经营贷款','流动资金贷款','周转贷款'];
        var yesNo=['否','是'];
        var d1=productItem.map(function (item,index) {
            return '<div class="card-header"><dl class="detail-bank"><dt> <img class="detail-bank_img" src="http://localhost:8888/imagepath/'+item.bankName+'.png"></dt>' +
                '<dd><h2>'+item.productName+'</h2><p>发布机构：'+item.bankName+'</p></dd></dl></div><div class="card-body card-body--padding">' +
                '<table class="table-detal"><tbody><tr><td class="table-th" style="width: 150px">参考利率范围：</td><td><em>'+item.rateLowerLimit+'%-'+item.rateUpperLimit+'%</em></td>' +
                '</tr><tr><td class="table-th" style="width: 150px">是否网贷直联：\t</td><td><em>'+yesNo[item.internetLoanType]+'</em></td></tr><tr><td class="table-th" style="width: 150px">是否政策性产品：</td>' +
                '<td><em>'+yesNo[item.productProperties]+'</em></td></tr><tr><td class="table-th" style="width: 150px">是否通用产品：</td>' +
                '<td><em>'+yesNo[item.generality]+'</em></td></tr><tr><td class="table-th" style="width: 150px">是否人民币：</td><td><em>'+yesNo[item.paymentType]+' </em></td>' +
                '</tr><tr><td class="table-th" style="width: 150px">贷款类别：</td><td><em>'+loanType[item.loanType]+'</em></td>' +
                '</tr><tr><td class="table-th" style="width: 150px">贷款额度：</td><td><em>'+Limit[item.loanLimit]+'</em></td>' +
                '</tr><tr><td class="table-th" style="width: 150px">贷款期限：</td><td><em>'+item.loanPeriod+'月</em></td>' +
                '</tr><tr><td class="table-th" style="width: 150px">担保方式：\t</td><td>'+guaranteeMode[item.guaranteeMode]+' </td></tr><tr>' +
                '<td class="table-th" style="width: 150px">适用设区市：</td><td>'+item.useArea+'</td>' +
                '</tr><tr><td class="table-th" style="width: 150px">备注：</td>' +
                '<td>'+item.description+'</td>' +
                '</tr></tbody></table><div class="detail-price">' +
                '<a class="detail-price_btn apply">立即申请  </a><a class="detail-price_btn back" href="#">返回  </a></div></div>';
        });
        //<span class="detail-price_desc"> 融资用途：<input/></span>
        $(".card--detail").append(d1);
        $('.apply').click(function(){
            alert("企业用户请登陆后再控制台申请");
        })
        $(".back").click(function () {
            var href=window.location.hostname;
            window.location.href="http://"+href+":8888/index.html";
        })
    }else{
        var data=[];
        var aList=[];
        var clickNews=function(){
            for(var i=0;i<aList.length;i++){
                aList[i].onclick=function (e) {
                    var index=e.target.id;
                    localStorage.setItem("content",data[index].newDesc);
                    localStorage.setItem("title",data[index].newName);
                    localStorage.setItem("time",data[index].releaseTime.slice(0,10));
                    var href=window.location.hostname;
                    console.log(href)
                    window.location.href="http://"+href+":8888/newsDetail.html";
                }
            }
        }
        var newsUl=$("#news-ul");
        $.ajax({
            type: "POST",
            url: 'http://localhost:8888/government/getNews',
            data: {
                pageNum:1,
                pageSize:10
            },
            success: function (result) {
                if(result.meta.success){
                    data=result.data["governmentNews List"];
                    var data1=data.map(function (item,index) {
                        return '<li class="news_item"><a href="javascript:void(0)" id="'+index+'">'+item.newName+'</a></li>';
                    })
                    newsUl.append(data1);
                    aList=$("#news-ul a");
                    clickNews();
                }else{
                    alert(result.meta.message);
                }
            },
            error:function(){
                alert("网络错误");
            }
        });
        var policyUl=$("#policies-list");
        $.ajax({
            type: "POST",
            url: 'http://localhost:8888/government/getPolicies',
            data: {
                pageNum:1,
                pageSize:10
            },
            success: function (result) {
                if(result.meta.success){
                    var d=result.data["governmentPolicies List"];
                    var data1=d.map(function (item,index) {
                        return '<li class="news_item"><a href="javascript:void(0)" id="'+index+'">'+item.policyName+'</a></li>';
                    })
                    policyUl.append(data1);
                    var policy=$("#policies-list a");
                    for(var i=0;i<policy.length;i++){
                        policy[i].onclick=function (e) {
                            var index=e.target.id;
                            localStorage.setItem("content",d[index].policyDesc);
                            localStorage.setItem("title",d[index].policyName);
                            localStorage.setItem("time",d[index].releaseTime.slice(0,10));
                            var href=window.location.hostname;
                            console.log(href)
                            window.location.href="http://"+href+":8888/policyDetail.html";
                        }
                    }
                }else{
                    alert(result.meta.message);
                }
            },
            error:function(){
                alert("网络错误");
            }
        });
        var resultsUl=$("#results-list");
        $.ajax({
            type: "POST",
            url: 'http://localhost:8888/bank/getProductDeal',
            success: function (result) {
                if(result.meta.success){
                    var d=result.data["Deal List"].slice(0,10);
                    var data1=d.map(function (item,index) {
                        return '<li class="news_item"><a href="javascript:void(0)" id="'+index+'">'+item.enterpriseName+'与'+item.bankName+'对接成功</a></li>';
                    })
                    resultsUl.append(data1);
                    // var results=$("#results-list a");
                    // for(var i=0;i<results.length;i++){
                    //     results[i].onclick=function (e) {
                    //         var index=e.target.id;
                    //         var href=window.location.hostname;
                    //         console.log(href)
                    //         window.location.href="http://"+href+":8888/policyDetail.html";
                    //     }
                    // }
                }else{
                    alert(result.meta.message);
                }
            },
            error:function(){
                alert("网络错误");
            }
        });
        var productsUl=$("#products-ul");
        $.ajax({
            type: "POST",
            url: 'http://localhost:8888/bank/dealStatics',
            success: function (result) {
                if(result.meta.success){
                    var d=result.data;
                    var enterpriseCount='<li class="news_item results-items"><a href="javascript:void(0)">注册企业<span style="color: red"> &nbsp;'+d.enterpriseCount+'&nbsp;</span> 家</a></li>'
                    var financial='<li class="news_item results-items"><a href="javascript:void(0)" >金融机构<span style="color: red"> &nbsp;'+d.bankCount+'</span> &nbsp;家,发布金融产品 &nbsp;<span style="color: red">'+d.financialProductCount+'</span> &nbsp;项</a></li>'
                    var request='<li class="news_item results-items"><a href="javascript:void(0)" >平台收到<span style="color: red">&nbsp;'+d.productRequestCount+'&nbsp;</span>笔企业融资需求,共<span style="color: red">&nbsp;'+d.productRequestAmountCount+'</span>&nbsp;万元</a></li>'
                    var deal='<li class="news_item results-items"><a href="javascript:void(0)" >机构解决<span style="color: red">&nbsp;'+d.dealProductCount+'&nbsp;</span>笔企业融资需求,共<span style="color: red">&nbsp;'+d.dealProductAmountCount+'</span>&nbsp;万元</a></li>'
                    productsUl.append(enterpriseCount);
                    productsUl.append(financial);
                    productsUl.append(request);
                    productsUl.append(deal);
                    // var results=$("#results-list a");
                    // for(var i=0;i<results.length;i++){
                    //     results[i].onclick=function (e) {
                    //         var index=e.target.id;
                    //         var href=window.location.hostname;
                    //         console.log(href)
                    //         window.location.href="http://"+href+":8888/policyDetail.html";
                    //     }
                    // }
                }else{
                    alert(result.meta.message);
                }
            },
            error:function(){
                alert("网络错误");
            }
        });
        //畅销产品
        $.ajax({
            type: "POST",
            url: 'http://localhost:8888/bank/getTopProduct',
            success: function (result) {
                if(result.meta.success){
                    var d=result.data.FinancialProductList;
                    var Limit=['100万及以下','200万及以下','300万及以下','500万及以下','1000万及以下','1000万以上'];
                    var guaranteeMode=['抵押','信保基金','一般保证','信用','实际控制人夫妇提供个人连带担保'];
                    var data=d.map(function (item,index) {
                        return '<dl class="products-item"><dt><a class="products-image_link" href="#"><img src="http://localhost:8888/imagepath/'+item.bankName+'.png"></a></dt>' +'<dd>' +
                            '<div class="products-title"><a class="products-title_link" href="">'+item.bankName+'-'+item.productName+'</a></div>' +
                            '<div class="products-info">' +
                            '<div class="products-type"><span class="products-type_item"> <i class="icon icon-type1"> </i><span>参考利率范围：</span><em>'+item.rateLowerLimit+'%-'+item.rateUpperLimit+'%</em></span><span class="products-type_item"> <i class="icon icon-type2"> </i><span>贷款额度：</span><em>'+Limit[item.loanLimit]+'</em></span><span class="products-type_item"> <i class="icon icon-type3"> </i><span>担保方式：</span><em>'+guaranteeMode[item.guaranteeMode]+'</em></span><span class="products-type_item">' +
                            '<i class="icon icon-type4"> </i><span>贷款期限：</span><em>'+item.loanPeriod+'月</em></span></div>' +
                            '<div class="products-action">' +
                            '<p class="btn-wrap"><a id="'+index+'" class="products-btn productsDesc" href="javascript:void(0)1">详情</a><a class="products-btn products-btn--red apply" href="#">申请' +
                            '</a></p>' +
                            '</div>' +
                            '</div>' +
                            '</dd>' +
                            '</dl>'
                    });
                    $(".products-list").append(data);
                    $(".products-list .products-item .apply").click(function () {
                        alert("企业用户请登陆后再控制台申请");
                    })
                    var descList=$(".products-list .productsDesc");
                    console.log(descList)
                    for(var i=0;i<descList.length;i++){
                        descList[i].onclick=function (e) {
                            localStorage.setItem("productItem",JSON.stringify(d[e.target.id]));
                            var href=window.location.hostname;
                            console.log(href);
                            window.location.href="http://"+href+":8888/productsDetail.html";
                        }
                    }
                }else{
                    alert(result.meta.message);
                }
            },
            error:function(){
                alert("网络错误");
            }
        });

        $("#company").click(function(){
            if($("#login-organization").is(":visible")){
                $("#login-organization").slideUp("fast");
            }
            if($("#login-government").is(":visible")){
                $("#login-government").slideUp("fast");
            }

            $("#login-company").slideToggle("fast");

        });
        // $("#admin").click(function(){
        //     if($("#login-organization").is(":visible")){
        //         $("#login-organization").slideUp("fast");
        //     }
        //     if($("#login-government").is(":visible")){
        //         $("#login-government").slideUp("fast");
        //     }
        //     if($("#login-company").is(":visible")){
        //         $("#login-company").slideUp("fast");
        //     }
        //
        //     $("#login-admin").slideToggle("fast");
        //
        // });
        // $("#admin").click(function(){
        //     event.stopPropagation();
        // });
        // $("#login-admin").click(function(){
        //     event.stopPropagation();
        // });

        $(document).click(function(){
            $("#login-company").slideUp("fast");
            $("#login-organization").slideUp("fast");
            $("#login-government").slideUp("fast");
            $("#login-admin").slideUp("fast");
        });
        // // 第二步： 将某元素的位置设为不可点击；(此部分点击不会触发点击事件)
        $("#company").click(function(){
            event.stopPropagation();
        });
        $("#login-company").click(function(){
            event.stopPropagation();
        });

        $("#organization").click(function(){
            if($("#login-company").is(":visible")){
                $("#login-company").slideUp("fast");
            }
            if($("#login-government").is(":visible")){
                $("#login-government").slideUp("fast");
            }
            if($("#login-admin").is(":visible")){
                $("#login-admin").slideUp("fast");
            }
            $("#login-organization").slideToggle("fast");
        });
        // $(document).click(function(){
        //   $("#login-organization").slideUp("slow");
        // });
        // // 第二步： 将某元素的位置设为不可点击；(此部分点击不会触发点击事件)
        $("#organization").click(function(){
            event.stopPropagation();
        });
        $("#login-organization").click(function(){
            event.stopPropagation();
        });

        $("#government").click(function(){
            if($("#login-company").is(":visible")){
                $("#login-company").slideUp("fast");
            }
            if($("#login-organization").is(":visible")){
                $("#login-organization").slideUp("fast");
            }
            if($("#login-admin").is(":visible")){
                $("#login-admin").slideUp("fast");
            }
            $("#login-government").slideToggle("fast");
        });
        // $(document).click(function(){
        //   $("#login-government").slideUp("slow");
        // });
        // // 第二步： 将某元素的位置设为不可点击；(此部分点击不会触发点击事件)
        $("#government").click(function(){
            event.stopPropagation();
        });
        $("#login-government").click(function(){
            event.stopPropagation();
        });


        $("#password").keydown(function (event) {
            if (event.keyCode == 13) {
                $("#login-submit").click();
            }
        });
        $("#login-submit").click(function(){
            var comUsername=$("#username").val();
            $.ajax({
                type: "POST",
                url: 'http://localhost:8888/enterprise/login',
                data: {
                    name:comUsername,
                    password:$("#password").val()
                },
                dataType: "json",
                success: function (result) {
                    if(result.meta.success){
                        window.localStorage.setItem("comUsername", comUsername);
                        window.sessionStorage.setItem("loginPath","company.html");
                        sessionStorage.setItem("user_type","CO")
                        sessionStorage.setItem("isLogin","true");
                        location.href = "company.html";
                    }else{
                        alert(result.meta.message);
                    }
                },
                error:function(){
                    alert("网络错误");
                }
            });
        });

        $("#fipassword").keydown(function (event) {
            if (event.keyCode == 13) {
                $("#filogin-submit").click();
            }
        });
        $("#filogin-submit").click(function(){
            if(!window.localStorage){
                console.log("浏览器不支持localstorage");
            }else{
                // var bkName = $("#bankName").val();
                var userName = $("#fiusername").val();

                // alert(bkName);
                // window.localStorage.setItem("bankName", bkName);
                window.localStorage.setItem("fiuserName", userName);
                window.sessionStorage.setItem("loginPath","financialInstitution.html");
            }
            $.ajax({
                type: "POST",
                url: 'http://localhost:8888/bank/login',
                data: {
                    // bankname:$("#bankName").val(),
                    name:$("#fiusername").val(),
                    password:$("#fipassword").val()
                },
                dataType: "json",
                // xhrFields: {withCredentials: true},
                // crossDomain: true,
                success: function (result) {

                    if(result.meta.success){
                        sessionStorage.setItem("user_type","FI")
                        window.location.href = "financialInstitution.html";
                    }else{
                        alert(result.meta.message);
                    }
                },
                error:function(){
                    alert("网络错误");
                }
            });
        });

        $("#gopassword").keydown(function (event) {
            if (event.keyCode == 13) {
                $("#gologin-submit").click();
            }
        });
        $("#gologin-submit").click(function(){
            var userName = $("#gousername").val();
            window.localStorage.setItem("gousername", userName);
            window.sessionStorage.setItem("loginPath","government.html");
            $.ajax({
                type: "POST",
                url: 'http://localhost:8888/government/login',
                data: {
                    name:$("#gousername").val(),
                    password:$("#gopassword").val()
                },
                dataType: "json",
                success: function (result) {

                    if(result.meta.success){
                        sessionStorage.setItem("user_type","GO")
                        window.location.href = "government.html";
                    }else{
                        alert(result.meta.message);
                    }
                },
                error:function(){
                    alert("网络错误");
                }
            });
        });
        $("#adminpassword").keydown(function (event) {
            if (event.keyCode == 13) {
                $("#admin-submit").click();
            }
        });


        $(".apply").click(function(){
            alert("请登录！");
        })
        $(".into").click(function () {
            var type=sessionStorage.getItem("user_type")
            var url=["company.html","financialInstitution.html","government.html"];
            if(type=="CO"){
                window.location.href=url[0];
            }else if(type=="FI"){
                window.location.href=url[1];
            }else if(type=="GO"){
                window.location.href=url[2];
            }

        })
    }
});


